package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.csaba.connector.model.Account;
import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.DynamicColumnLayout;
import com.csaba.swing.gui.util.TableSizeContainer;
import com.csaba.swing.gui.util.WindowSizeContanier;
import com.csaba.swing.model.AccountCache;

@SuppressWarnings( "serial" )
public class CustomerView extends JInternalFrame implements ContextListener
{
	//private final static Logger logger = LoggerHelper.getDefaultLogger();

	JLabel id;
	JLabel loginId;
	JLabel name;
	JTable nativeFields;
	Customer customer;

	public CustomerView()
	{
		super(MainWindow.getString("CustomerView.title"), true, true, true, true); //$NON-NLS-1$
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		if ( !WindowSizeContanier.getInstance().monitorComponent(this) )
			setSize(400, 350);
		setFrameIcon(GUIContext.getIcon("profile", 16)); //$NON-NLS-1$

		//init layout
		final JPanel general = new JPanel(new DynamicColumnLayout(new int[] { DynamicColumnLayout.RIGHT,
				DynamicColumnLayout.LEFT }));
		general.add(new JLabel(MainWindow.getString("CustomerView.customerId"))); //$NON-NLS-1$
		general.add(id = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("CustomerView.loginId"))); //$NON-NLS-1$
		general.add(loginId = new JLabel("")); //$NON-NLS-1$
		general.add(new JLabel(MainWindow.getString("CustomerView.name"))); //$NON-NLS-1$
		general.add(name = new JLabel("")); //$NON-NLS-1$

		final JPanel buttons = new JPanel();
		buttons.add(new JButton(new AbstractAction(MainWindow.getString("CustomerView.button.accounts")) //$NON-NLS-1$
		{

			@Override
			public void actionPerformed( final ActionEvent arg0 )
			{
				if ( customer != null )
				{
					final Session session = GUIContext.getInstance().getSession(customer);
					if ( session != null )
					{
						final Account[] accounts = AccountCache.getInstace().get(session);

						final AccountListView view = new AccountListView();
						MainWindow.getDesktop().add(view);
						view.showIt(customer, accounts);
					}
				}
			}

		}));
		buttons.add(new JButton(new AbstractAction(MainWindow.getString("CustomerView.button.close")) //$NON-NLS-1$
		{

			@Override
			public void actionPerformed( final ActionEvent arg0 )
			{
				dispose();
			}

		}));

		nativeFields = new JTable(new Object[0][2], new String[] { MainWindow.getString("CustomerView.header.name"), MainWindow.getString("CustomerView.header.value") }); //$NON-NLS-1$ //$NON-NLS-2$
		final JScrollPane scrollPane = new JScrollPane(nativeFields);
		nativeFields.setFillsViewportHeight(true);

		final JPanel contentPane = (JPanel) getContentPane();
		contentPane.setBorder(BorderFactory.createEmptyBorder());
		contentPane.setLayout(new BorderLayout());
		contentPane.add(general, BorderLayout.NORTH);
		contentPane.add(buttons, BorderLayout.SOUTH);
		contentPane.add(scrollPane, BorderLayout.CENTER);

		GUIContext.getInstance().addContextListener(this);
	}

	public void showIt( final Customer customer )
	{
		this.customer = customer;
		id.setText(customer.getId());
		loginId.setText(customer.getLoginId());
		name.setText(customer.getName());

		final String[] propNames = customer.getRemotePropertyNames();
		Arrays.sort(propNames);
		final String[][] tableModel = new String[propNames.length][2];
		for ( int i = 0; i < tableModel.length; i++ )
		{
			tableModel[i][0] = customer.getLocalizedName(propNames[i]);
			tableModel[i][1] = customer.getRemoteProperty(propNames[i]).toString();
		}
		nativeFields.setModel(new DefaultTableModel(tableModel, new String[] { MainWindow.getString("CustomerView.header.name"), MainWindow.getString("CustomerView.header.value") }) //$NON-NLS-1$ //$NON-NLS-2$
		{
			@Override
			public boolean isCellEditable( final int row, final int column )
			{
				return false;
			}
		});

		setVisible(true);
		TableSizeContainer.getInstance().monitorTable("CustomerView.nativeFields", nativeFields); //$NON-NLS-1$
	}

	@Override
	public void contextEventPerformed( final int eventType )
	{
		if ( eventType == SESSION_REMOVED )
		{
			final List<Session> sessions = GUIContext.getInstance().getSessions();
			boolean sessionStillActive = false;
			for ( final Session session : sessions )
			{
				if ( session.getCustomer() == customer )
				{
					sessionStillActive = true;
					break;
				}
			}
			if ( !sessionStillActive )
			{
				dispose();
			}
		}

	}
}
